#include <stdlib.h>

#include "tpm/astro.h"

#include "coord_conv.h"


/* int */
/* mmmmain(int agc, char**argv) */
/* { */
/*     V6 vi;	/\* working vector *\/ */
/*     V6 vo;	/\* scratch vector *\/ */

/*     vi = v6init(SPHERICAL); */
/*     v6SetR(vi, 1.e2); */
/*     v6SetAlpha(vi, d2r(93.350503)); */
/*     v6SetDelta(vi, d2r(22.614092)); */

/*     //v6 = gal2equ(v6); */
/*     //vo = ; */
/*     vo = v6c2s(equ2gal(vi)); */

/*     //v6SetR(vi, ); */
/*     printf("## %f, %f\n", 360.+r2d(v6GetAlpha(vo)), r2d(v6GetDelta(vo))); */
/*     //v6 = fk425(v6); */
/*     //v6 = fk524(v6); */

/*     vo = v6c2s(fk425(vi)); */

/*     //v6SetR(vi, ); */
/*     printf("## %f, %f\n", r2d(v6GetAlpha(vo)), r2d(v6GetDelta(vo))); */

/* } */





coord_conv_struct *coord_conv_struct_new(coord_conv_func func, 
					 void *data, void (*data_destr)(void *)) {
  coord_conv_struct * r;
  
  r = (coord_conv_struct *) malloc(sizeof(coord_conv_struct));
  r->func = func;
  r->data = data;
  r->data_destr = data_destr;

  return r;
}

void coord_conv_struct_free(void *o) {
  coord_conv_struct * r = o;
  
  if (r->data_destr) {
    r->data_destr(r->data);
  }

  free(r);

}



int
fk5_gal(double ra, double dec, double *l, double *b)
{
    V6 vi;	/* working vector */
    V6 vo;	/* scratch vector */

    vi = v6init(SPHERICAL);
    v6SetR(vi, 1.e2);
    v6SetAlpha(vi, d2r(ra));
    v6SetDelta(vi, d2r(dec));


    vo = fk524(vi);

    vo = v6c2s(equ2gal(vo));

    *l = r2d(v6GetAlpha(vo));
    //if (*l < 0.) *l += 360.;
    *b = r2d(v6GetDelta(vo));
    
    return 1;
}

int
gal_fk5(double l, double b, double *ra, double *dec)
{
    V6 vi;	/* working vector */
    V6 vo;	/* scratch vector */

    vi = v6init(SPHERICAL);
    v6SetR(vi, 1.e2);
    v6SetAlpha(vi, d2r(l));
    v6SetDelta(vi, d2r(b));


    vo = gal2equ(vi);

    vo = v6c2s(fk425(vo));

    *ra = r2d(v6GetAlpha(vo));
    *dec = r2d(v6GetDelta(vo));

    return 1;
}




int
fk4_gal(double ra, double dec, double *l, double *b)
{
    V6 vi;	/* working vector */
    V6 vo;	/* scratch vector */

    vi = v6init(SPHERICAL);
    v6SetR(vi, 1.e2);
    v6SetAlpha(vi, d2r(ra));
    v6SetDelta(vi, d2r(dec));


    vo = v6c2s(equ2gal(vi));

    *l = r2d(v6GetAlpha(vo));
    *b = r2d(v6GetDelta(vo));

    return 1;
}

int
gal_fk4(double l, double b, double *ra, double *dec)
{
    V6 vi;	/* working vector */
    V6 vo;	/* scratch vector */

    vi = v6init(SPHERICAL);
    v6SetR(vi, 1.e2);
    v6SetAlpha(vi, d2r(l));
    v6SetDelta(vi, d2r(b));


    vo = v6c2s(gal2equ(vi));

    *ra = r2d(v6GetAlpha(vo));
    *dec = r2d(v6GetDelta(vo));

    return 1;
}



int
fk5_fk4(double ra, double dec, double *l, double *b)
{
    V6 vi;	/* working vector */
    V6 vo;	/* scratch vector */

    vi = v6init(SPHERICAL);
    v6SetR(vi, 1.e2);
    v6SetAlpha(vi, d2r(ra));
    v6SetDelta(vi, d2r(dec));


    vo = v6c2s(fk524(vi));

    *l = r2d(v6GetAlpha(vo));
    *b = r2d(v6GetDelta(vo));

    return 1;
}

int
fk4_fk5(double l, double b, double *ra, double *dec)
{
    V6 vi;	/* working vector */
    V6 vo;	/* scratch vector */

    vi = v6init(SPHERICAL);
    v6SetR(vi, 1.e2);
    v6SetAlpha(vi, d2r(l));
    v6SetDelta(vi, d2r(b));


    vo = v6c2s(fk425(vi));

    *ra = r2d(v6GetAlpha(vo));
    *dec = r2d(v6GetDelta(vo));

    return 1;
}
